package com.jiahao.material.controllers;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiahao.material.entity.Permission;
import com.jiahao.material.entity.Result;
import com.jiahao.material.entity.Role;
import com.jiahao.material.entity.RolePermission;
import com.jiahao.material.mapper.RoleMapper;
import com.jiahao.material.mapper.RolePermissionMapper;
import com.jiahao.material.services.IPermisisonService;
import com.jiahao.material.utils.ResultUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    private IPermisisonService permissionService;

    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    @RequiresPermissions("addPermission")
    @PostMapping("/add")
    public Result add(@RequestBody Permission permission) throws Exception{
        permission.setStatus("01");
        permissionService.save(permission);
        return ResultUtil.success("添加权限成功!");
    }

    @GetMapping("/byId/{id}")
    public Result getById(@PathVariable String id) throws Exception{
        Permission byId = permissionService.getById(id);
        return ResultUtil.success("",byId);
    }

    @GetMapping("/activeAll")
    public Result getByActiveAll() throws Exception{
        QueryWrapper<Permission> permissionQueryWrapper = new QueryWrapper<>();
        permissionQueryWrapper.eq("status","01");
        List<Permission> list = permissionService.list(permissionQueryWrapper);
        return ResultUtil.success("",list);
    }


    /**
     * 分页查询权限
     * @param map
     * @return
     * @throws Exception
     */
    @PostMapping("/page")
    public Result getByPage(@RequestBody Map<String,Object> map) throws Exception{
        int pageSize = map.get("pageSize") == null ? 10 : Integer.parseInt(map.get("pageSize").toString());
        int currentPage = map.get("currentPage") == null ? 1 : Integer.parseInt(map.get("currentPage").toString());
        Page<Permission> page = new Page<>(currentPage,pageSize);
        IPage<Permission> byPage = permissionService.getByPage(page, map);
        return ResultUtil.success("",byPage);
    }

    @RequiresPermissions("updatePermission")
    @PutMapping("/update")
    public Result update(@RequestBody Permission permission) throws Exception{
        permissionService.updateById(permission);
        return ResultUtil.success("更新权限成功!");
    }


    @PostMapping("/checkCode")
    public Result checkCode(@RequestBody Map<String,Object> map) throws Exception{
        QueryWrapper<Permission> permissionQueryWrapper = new QueryWrapper<>();
        permissionQueryWrapper.eq("code",map.get("code").toString());
        Permission one = permissionService.getOne(permissionQueryWrapper);
        String e="0";
        if (one!=null)
            e = one.getId().equals(map.get("id").toString())?"0":"1";
        return ResultUtil.success(e);
    }

    /**
     * 更新状态
     * @param map
     * @return
     * @throws Exception
     */
    @PutMapping("/status")
    @Transactional
    public Result updateStatus(@RequestBody Map<String,Object> map) throws Exception{
        UpdateWrapper<Permission> permissionUpdateWrapper = new UpdateWrapper<>();
        permissionUpdateWrapper.eq("id",map.get("id").toString()).set("status",map.get("status").toString());
        permissionService.update(permissionUpdateWrapper);
        return ResultUtil.success("更新状态成功!");
    }

    @RequiresPermissions("delPermission")
    @DeleteMapping("/del/{id}")
    @Transactional
    public Result delById(@PathVariable String id) throws Exception{

        //先删除角色与权限的关系
        QueryWrapper<RolePermission> roleMapperQueryWrapper = new QueryWrapper<>();
        roleMapperQueryWrapper.eq("pid",id);
        rolePermissionMapper.delete(roleMapperQueryWrapper);

        permissionService.removeById(id);
        return ResultUtil.success("删除权限成功!");
    }

    @RequiresPermissions("delPermission")
    @PostMapping("/dels")
    @Transactional
    public Result delByIds(@RequestBody List<String> ids) throws Exception{
        //先删除角色与权限的关系
        QueryWrapper<RolePermission> rolePermissionQueryWrapper = new QueryWrapper<>();
        rolePermissionQueryWrapper.in("pid",ids);
        rolePermissionMapper.delete(rolePermissionQueryWrapper);

        permissionService.removeByIds(ids);
        return ResultUtil.success("删除权限成功!");
    }

}
